package 网络编程;

import org.junit.Test;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class TCP03 { //客户端发送文件给服务端,服务端将文件保存在本地

    //客户端
    @Test
    public void client(){
        Socket socket = null;
        OutputStream outputStream = null;
        InputStream inputStream = null;

        try {
            socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
            outputStream = socket.getOutputStream();

            //读取文件
            inputStream = new BufferedInputStream(new FileInputStream(new File("E:\\FileTest\\成绩合格证明.png")));
            byte[] array = new byte[1024];
            int len;
            while((len = inputStream.read(array)) != -1){
                outputStream.write(array,0,len);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (outputStream != null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //服务端
    @Test
    public void server(){
        ServerSocket serverSocket = null;
        Socket accept = null;
        InputStream inputStream = null;
        OutputStream outputStream = null;

        try {
            serverSocket = new ServerSocket(9090);
            accept = serverSocket.accept();
            inputStream = accept.getInputStream();
            outputStream = new BufferedOutputStream(new FileOutputStream(new File("E:\\FileTest\\copy1.png")));

            byte[] array = new byte[1024];
            int len;
            while ((len = inputStream.read(array)) != -1){
                outputStream.write(array,0,len);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(outputStream != null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(accept != null){
                try {
                    accept.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(serverSocket != null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
